/*
 * To change this license header, choose License Headers in Project Properties. To change this
 * template file, choose Tools | Templates and open the template in the editor.
 */
package com.party.file.cos.demo;

import com.party.file.cos.COSClient;
import com.party.file.cos.ClientConfig;
import com.party.file.cos.request.StatFolderRequest;
import com.party.file.cos.sign.Credentials;


/**
 * @author chengwu cos Demo代码
 */
public class Demo1 {

    public static void main(String[] args) throws Exception {

        // 设置用户属性, 包括appid, secretId和SecretKey
        // 这些属性可以通过cos控制台获取(https://console.qcloud.com/cos)
//        long appId = 10052192;
//        String secretId = "AKID0zLb467fRifqb0rsyo5tVJKZqKiebe5B";
//        String secretKey = "UcfDJpkyGi90IDw0YsnxxUcLkWopbrC6";
//        // 设置要操作的bucket
//        String bucketName = "txzapp";

//        long appId = 1255395276;
//        String secretId = "AKIDQMCuLiwyneNuEMrzpUwBD8uV3PZWvoAJ";
//        String secretKey = "5a0hGdoncFk7JORMAnGXbaOgvypUwCF9";
//        // 设置要操作的bucket
//        String bucketName = "tongxingzhe";


        long appId = 1255619907;
        String secretId = "AKIDjMuKZmVqhPvGPNsF1c2HMJCzgGk8qgqu";
        String secretKey = "784uz38ObgfxAGID209ILHFKLG4lLrHa";
        // 设置要操作的bucket
        String bucketName = "zljapp";

        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域，比如广州(gz), 天津(tj)
        clientConfig.setRegion("gz");
        // 初始化秘钥信息
        Credentials cred = new Credentials(appId, secretId, secretKey);
        // 初始化cosClient
        COSClient cosClient = new COSClient(clientConfig, cred);

        // 3. 上传文件(上传远程文件)
        // 示例程序的内存数据来自于本地文件，将本地文件整体读入内存，如果本地文件过大，可能导致OOM
        // 因此如果数据在本地，建议直接用方法1
//        String remoteUrl = "http://txzapp-10052192.image.myqcloud.com/1512458613335";
//        byte[] contentBuffer = ImgUtil.getImageFromNetByUrl(remoteUrl);
//        for(int i=0; i<1000; i++) {
//            UploadFileRequest overWriteFileRequest =
//                    new UploadFileRequest(bucketName, "/test/test/test/test/"+i+".jpg", contentBuffer);
//            // 如果COS上已有文件, 则进行覆盖(默认不覆盖)
//            overWriteFileRequest.setInsertOnly(InsertOnly.OVER_WRITE);
//            String overWriteFileRet = cosClient.uploadFile(overWriteFileRequest);
//            System.out.println("overwrite file ret:" + overWriteFileRet);
//        }
//
        StatFolderRequest statFolderRequest = new StatFolderRequest(bucketName, "/333/");
        String statFolderRet = cosClient.statFolder(statFolderRequest);
        System.out.println("stat folder ret:" + statFolderRet);

//        String cosFolderPath = "/xxsample_folder/";
//        CreateFolderRequest createFolderRequest =
//                new CreateFolderRequest(bucketName, cosFolderPath);
//        String createFolderRet = cosClient.createFolder(createFolderRequest);
//        System.out.println("create folder ret:" + createFolderRet);

        // 关闭释放资源
        cosClient.shutdown();
        System.out.println("shutdown!");
    }
}
